<?php

namespace app\admin\controller\supplier;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Session;

/**
 * @ControllerAnnotation(title="mall_order")
 */
class Order extends AdminController
{

    use \app\admin\traits\Curd;
    protected $order_id;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\mall\Order();
        
        $this->order_id = Session::get('order_id') ??0;
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->where('pay_status',1)
                ->where('indent_status',0)
                ->count();
            $list = $this->model
                ->where($where)
                ->where('pay_status',1)
                ->where('indent_status',0)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                $goodsModel = new \app\admin\model\mall\Goods;
                $goodsData = $goodsModel->where('is_default',1)->find();
                foreach ($list as $key => &$value) {
                    // $value['salesman_remind'] = ($value['salesman_remind']==0) ?$goodsData['goods_remind']:$value['salesman_remind'];
                    // $value['shop_remind'] = ($value['shop_remind']==0) ?$goodsData['goods_remind']:$value['shop_remind'];
                    $value['goods_name'] = $goodsData->name;
                    $value['goods_images'] = $goodsData->images;
                }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->layoutBgColor();
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="订单物流列表")
     */
    public function logistics_list()
    {
        $order_id = $this->request->get('order_id') ??0;
        // print_r($order_id); exit;
        Session::set('order_id', $order_id);
        if ($this->request->isAjax()) {
            if ($this->order_id == 0) {
                $this->error('订单编号错误');
            }
            // print_r(Session::get('order_id'));
            //  exit;
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            // print_r($this->order_id); exit;
            $orderLogisticsModel = new \app\admin\model\mall\OrderLogistics();
            $count = $orderLogisticsModel
                ->where('order_id',$this->order_id)
                ->count();
            $list = $orderLogisticsModel
                ->where('order_id',$this->order_id)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->layoutBgColor();
        return $this->fetch();
    }
    
}